/*
 * cPlayer.cpp
 *
 *  Created on: 19.05.2012
 *      Author: NOR00
 */

#include "cPlayer.h"

namespace ants {

cPlayer::cPlayer() {
}

cPlayer::cPlayer(int x, int y, cTexture* tex)
{
	m_x = x;
	m_y = y;
	m_spd = 4.0f;
	Initialize(tex);

	m_state = PlayerState_Wait;

}

void cPlayer::Move(int x, int y)
{
	bool isMove = true;
	cEntity::Move(x, y);

	if (x<0)
	{
		m_curAnim = m_anims["WalkLeft"];
	}
	else if (x>0)
	{
		m_curAnim = m_anims["WalkRight"];
	}
	else if (y<0)
	{
		m_curAnim = m_anims["WalkUp"];
	}
	else if (y>0)
	{
		m_curAnim = m_anims["WalkDown"];
	}
	else
	{
		isMove = false;
	}
}

void cPlayer::Initialize(cTexture* tex)
{
	int sdvig = 0;

	cAnimation* anim = SAnimationManager->CreateAnimation(tex, sdvig+2, sdvig+5);
	anim->SetRepeations(-1);
	AddAnimation("WalkDown", anim);

	sdvig += 6;

	anim = SAnimationManager->CreateAnimation(tex, sdvig+2, sdvig+5);
	anim->SetRepeations(-1);
	AddAnimation("WalkLeft", anim);

	sdvig += 6;
	anim = SAnimationManager->CreateAnimation(tex, sdvig+2, sdvig+5);
	anim->SetRepeations(-1);
	AddAnimation("WalkUp", anim);


	sdvig += 6;
	anim = SAnimationManager->CreateAnimation(tex, sdvig+2, sdvig+5);
	anim->SetRepeations(-1);
	AddAnimation("WalkRight", anim);

	m_curAnim = m_anims["WalkDown"];

	SetWidth(32);
	SetHeight(64);
}

cPlayer::~cPlayer() {
	//Delete();
}

void cPlayer::Control()
{
	bool isWalkStart = true;
	if (SInput->IsKeyDown(KEY_UP))
	{
		SetDirection(EntityDirection_UP);
	}
	else if (SInput->IsKeyDown(KEY_DOWN))
	{
		SetDirection(EntityDirection_DOWN);
	}
	else if (SInput->IsKeyDown(KEY_LEFT))
	{
		SetDirection(EntityDirection_LEFT);
	}
	else if (SInput->IsKeyDown(KEY_RIGHT))
	{
		SetDirection(EntityDirection_RIGHT);
	}
	else
	{
		isWalkStart = false;
		m_state = PlayerState_WalkStart;
	}

	bool isWalk = true;

	if (!SInput->IsKeyHeld(KEY_UP) && !SInput->IsKeyHeld(KEY_DOWN) &&
			!SInput->IsKeyHeld(KEY_LEFT) && !SInput->IsKeyHeld(KEY_RIGHT))
	{
		isWalk = false;
		m_state = PlayerState_Walk;
	}

//	if (SInput->IsKeyHeld(KEY_UP))
//	{
//		Move(0, -m_spd);
//	}
//	else if (SInput->IsKeyHeld(KEY_DOWN))
//	{
//		Move(0, m_spd);
//	}
//	else if (SInput->IsKeyHeld(KEY_LEFT))
//	{
//		Move(-m_spd, 0);
//	}
//	else if (SInput->IsKeyHeld(KEY_RIGHT))
//	{
//		Move(m_spd, 0);
//	}
//	else
//	{
//		isWalk = false;
//		m_state = PlayerState_Walk;
//	}
}

void cPlayer::Update()
{
	Control();

	switch (m_state)
	{
	case PlayerState_WalkStart:
		m_curAnim->Start(-1);
		break;
	case PlayerState_Walk:

		switch (m_direction)
		{
		case EntityDirection_DOWN:
			Move(0, m_spd);
			break;
		case EntityDirection_LEFT:
			Move(-m_spd, 0);
			break;
		case EntityDirection_RIGHT:
			Move(m_spd, 0);
			break;
		case EntityDirection_UP:
			Move(0, -m_spd);
			break;
		}

		break;
	case PlayerState_Wait:
		break;
	}

//	switch (m_state)
//	{
//	case PlayerState_Wait:
//		m_curAnim->Reset();
//		//m_curAnim->Pause();
//		break;
//	case PlayerState_Walk:
//		//m_curAnim->Resume();
//	break;

}

}
